package com.bf.demo.util;

import org.jasypt.encryption.pbe.StandardPBEStringEncryptor;
import org.jasypt.encryption.pbe.config.EnvironmentPBEConfig;

public class JasyptTest {

    /**
     * 加密
     * @throws Exception
     */
    public static void testEncrypt() throws Exception {
        StandardPBEStringEncryptor standardPBEStringEncryptor = new StandardPBEStringEncryptor();
        EnvironmentPBEConfig config = new EnvironmentPBEConfig();
        config.setAlgorithm("PBEWithMD5AndDES");          // 加密的算法，这个算法是默认的
        config.setPassword("97e42876-b70c-8589-9bde-cb6710102add"); // 加密的盐
        // 加密的密钥，随便自己填写，很重要千万不要告诉别人
        standardPBEStringEncryptor.setConfig(config);
        String plainText = "2021000117670372";         //需要加密的信息
        String encryptedText = standardPBEStringEncryptor.encrypt(plainText);
        System.out.println(encryptedText);
    }

    /**
     * 解密
     * @throws Exception
     */
    public static void testDe() throws Exception {
        StandardPBEStringEncryptor standardPBEStringEncryptor = new StandardPBEStringEncryptor();
        EnvironmentPBEConfig config = new EnvironmentPBEConfig();
        config.setAlgorithm("PBEWithMD5AndDES");
        config.setPassword("97e42876-b70c-8589-9bde-cb6710102add");
        standardPBEStringEncryptor.setConfig(config);
        String encryptedText = "OJnj7081n472vFCwixWH+FhyKkgzy31j";   //加密后的密码
        String plainText = standardPBEStringEncryptor.decrypt(encryptedText);
        System.out.println(plainText);
    }

    public static void main(String[] args) {
        try {
            JasyptTest.testEncrypt();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}